/*************************************************
 *FileName:      dataattestation.js
 *Description:   约会认证.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'refreshindex');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var that = getUrlParam('that');
    var dataid = getUrlParam('dataid');
    var token = localStorage.replace_token;
    var uid = localStorage.replace_uid;
    var activity_province , activity_city ,activity_district ;
    var status;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        if(localStorage.replace_sex=='woman'){
            $('.content-img').html('<img src="images/gilr.png"><p class="content-p">示范照片</p>')
        }else{
            $('.content-img').html('<img src="images/boy.png"><p class="content-p">示范照片</p>')
        }
        if(that){
            $('.mui-title').text('约会认证申请资料');
            $('.btn a').css({display:'block'});
            $('input').attr('disabled',true);
        }else{
            $('.mui-title').text('约会认证');
            $('.rdntn').css({display:'block'});
        }
        mui.plusReady(state)
//      state();
        $('.attestation').on('tap','.cancel',function(){
            if(status==''||status==2||status==3){
                $(this).parent().html('<p class="text"><span>1</span></span>/1</p>');
            }
            return false;
        })
        
        $('.img-look').on('tap',function(){
            $(this).removeClass('active');
        })
        
        //input输入的时候输入框跟着弹上去
        $('input').on('click', function () {
            var target = this;
            $(".mui-content").css('-webkit-overflow-scrolling','auto'); 
            setTimeout(function(){
                target.scrollIntoViewIfNeeded();
                $(".mui-content").css('-webkit-overflow-scrolling','touch');
                console.log('scrollIntoViewIfNeeded');
            },400);
        });
        
        $('.attestation').on('tap','.add-img',function(){
            if(dataid){
                var osrc =$(this).find('img').attr('src');
                $('.img-look').addClass('active');
                $('.img-look-box').html('<img src="'+osrc+'"/>')
            }else if(status==''||status==2||status==3){
                captureImage(this);
            }
        })
        
        $('.attestation').on('tap','.table-view-cell-a1',function(){
            $('.table-view3').fadeOut();
            $('.table-view1').fadeIn();
            $('.popover').css({display :'block'});
        })
        
        $('.attestation .table-view3').on('tap','.table-view-cell-a2',function(){
            $('.table-view3').fadeOut();
            $('.popover').css({display :'none'});
            var str ='平台';
            upload(0,str);
        })
        
        $('.attestation .table-view1').on('tap','.table-view-cell-affirm',function(){
            $('.table-view1').fadeOut();
            $('.popover').css({display :'none'});
            var muid =$('.table-view-cell-input').val();
            $('.table-view-cell-input').blur();
            var str ='经理人';
            cord(muid,str);
        })
        
        $('.attestation').on('tap','.table-view-cell-cancel',function(){
            $('.table-view').fadeOut();
            $('.popover').css({display :'none'});
        })
        
        var _getParam = function(obj, param) {return obj[param] || '';};
        var cityPicker3 = new mui.PopPicker({layer: 3});
        cityPicker3.setData(cityData3);
        $('.personal').on('focusin','.range2', function(e) {
            $(this).blur();
            cityPicker3.show(function(items) {
                activity_province = _getParam(items[0], 'text');
                activity_city = _getParam(items[1], 'text');
                activity_district = _getParam(items[2], 'text');
                $('.range').val(activity_province+'-'+activity_city+'-'+activity_district);
            });
        })
    }
    
    //点击判断
    var oname,ophone,owx,oqq,orange ,ocompany,oadderss,img ,img1;
    $('.rdntn').on('tap',function(){
        oname = $('.name').val();
        ophone = $('.phone').val();
        owx = $('.wx').val();
        oqq = $('.qq').val();
        orange = $('.range').val();
        ocompany = $('.company').val();
        oadderss = $('.adderss').val();
        img = $('.personal-img .add-img img').attr('src');
        img1 = $('.personal-video .add-img img').attr('src');
        if(status==1||status=='0'||$(this).hasClass('rmcolor')){
            return false; 
        }else if(!oname){ 
            mui.toast('请输入真实姓名！');
            return false; 
        }else if(   !(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(ophone) ) ){ //判断手机号
            mui.toast('请输入正确手机号码！');
            return false; 
        }else if(!owx){
            mui.toast('请输入微信号！');
            return false; 
        }else if(!orange){
            mui.toast('请选择活动范围！');
            return false; 
        }else if(!img||!img1){
            mui.toast('请上传认证照片！');
            return false; 
        }else{
            $('.table-view3').fadeIn();
            $('.popover').css({display :'block'});
        }
    })
    
    //推荐码判断获取
    function cord(ocord,str){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/dateInviteCodeCheck",
            dataType:'jsonp',
            data: {invite_code:ocord},
            success: function(data) {
                if(data.error_code == 0){
                    upload(data.data.uid,str);
                }else{
                    mui.toast(data.error_msg);
                }
            }  
        });
    }
    
    //上传
    function upload(manager_uid,str){
        var task = plus.uploader.createUpload(DOMAIN_NAME + "/index.php/api/users_verify/verifyadd",
        { method:"POST",blocksize:20480,priority:100 },
            function (t, status) { //上传完成
                common.loadingOut();
                if (status == 200) {
                    mui.alert('约会认证已提交'+str+'，请等待审核！','提示','确定',function(){setTimeout(function(){mui.back()},500);},'div');
                }else{
                    mui.toast('上传失败',{ duration:2000, type:'div' });
                }
            }
        );
        common.loadingIn();
        manager_uid==0? task.addData("manager_uid", '0'):task.addData("manager_uid", ''+manager_uid);
        task.addData("name", oname);
        task.addData("uid", uid);
        task.addData("token",token);
        task.addData("phone", ophone);
        task.addData("wx", owx);
        task.addData("qq", oqq);
        task.addData("open", orange);
        task.addData("activity_province", activity_province);
        task.addData("activity_city", activity_city);
        task.addData("activity_district", activity_district);
        task.addData("work_unit", ocompany);
        task.addData("work_address", oadderss);
        task.addFile(img, {key: "img"});
        task.addFile(img1, {key: "img1"});
        task.addData("type", "1");
        mui.toast('开始上传',{ duration:2000, type:'div' });
        task.start();
    }
    
    //状态获取
    function state(){
        if(that){
            var odata= { uid: uid, token: token,id : dataid};
            var ourl ="/index.php/api/Users_verify/usersDateVerify";
        }else{
            var odata= { uid: uid, token: token,type : 1};
            var ourl ="/index.php/api/users_verify/UsersVerify";
        }
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + ourl,
            dataType:'jsonp',
            data:odata ,
            success: function(data) {
                if(data.error_code == 0){
                    status =data.data.status;
                    if(status!=3&&status!=2){
                        $('.personal input').attr('readonly','readonly');
                        $('.range').removeClass('range2');
                    }
                    $('.name').val(data.data.name);
                    $('.phone').val(data.data.phone);
                    $('.wx').val(data.data.wx);
                    data.data.qq ==''?$('.qq').val(' '):$('.qq').val(data.data.qq);
                    $('.range').val(data.data.activity_province+'-'+data.data.activity_city+'-'+data.data.activity_district);
                    activity_province=data.data.activity_province;
                    activity_city=data.data.activity_city;
                    activity_district=data.data.activity_district;
                    data.data.work_unit ==''?$('.company').val(' '):$('.company').val(data.data.work_unit);
                    data.data.work_address ==''?$('.adderss').val(' '):$('.adderss').val(data.data.work_address);
                    $('.personal-img .add-img').html('<img src="'+DOMAIN_NAME+data.data.img+'"/><i class="cancel"></i>');
                    $('.personal-video .add-img').html('<img src="'+DOMAIN_NAME+data.data.img1+'"/><i class="cancel"></i>');
                    $('.content-img').remove();
                    if(status==1||status=='0'){
                        $('.rdntn').addClass('rmcolor');
                    }
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    
                }
            }  
        });
    }
    
    //拍照和相册
    function captureImage(othis){
        localStorage.camopen="false";
        common.cameraImg(function(src){
            $(othis).html('<img src="'+src+'"/><i class="cancel"></i>')
        })
    }
    
    //同意
    $('.attestation').on('tap','.agree',function(){
        mui.confirm('同意约会认证？','提示',['确定','取消'],function(e){
            if(e.index==0){
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + '/index.php/api/Users_verify/agreeVerifyApply',
                    dataType:'jsonp',
                    data:{uid:uid ,token:token,id :dataid} ,
                    success: function(data) {
                        if(data.error_code == 0){
                            mui.back();
                            var list = plus.webview.currentWebview().opener();//触发父页面的自定义事件(refresh),从而进行刷新
                            mui.fire(list, 'handlersharp');//返回true,继续页面关闭逻辑
                            return true;
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                                exitlogin();
                            },1000);
                        }else{
                            
                        }
                    }  
                });
            }
        },'div')
    })
    //拒绝
    $('.attestation').on('tap','.disagree',function(){
        mui.confirm('拒绝通过？','提示',['确定','取消'],function(e){
            if(e.index==0){
                $.ajax({
                    type: "post",
                    url: DOMAIN_NAME + '/index.php/api/Users_verify/refuseVerifyApply',
                    dataType:'jsonp',
                    data:{uid:uid ,token:token,id :dataid} ,
                    success: function(data) {
                        if(data.error_code == 0){
                            mui.back();
                            var list = plus.webview.currentWebview().opener();//触发父页面的自定义事件(refresh),从而进行刷新
                            mui.fire(list, 'handlersharp');//返回true,继续页面关闭逻辑
                            return true;
                        }else if(data.error_code == 1001){
                            setTimeout(function(){
                                exitlogin();
                            },1000);
                        }else{
                            
                        }
                    }  
                });
            }
        },'div')
    })
    /**
     * 
     * mui框架时间样式
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */